package com.ztgf.sys.api.cms;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.sys.models.bo.SysNoticeBO;
import com.ztgf.sys.models.dto.SysNoticeDTO;
import com.ztgf.sys.models.service.ISysNoticeService;
import com.ztgf.sys.models.vo.SysNoticeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 发送信息布告表控制器
 *
 * @author liyqng
 * @Date 2021-05-13 17:03:02
 */

@RestController
@RequestMapping("/cms/sysNotice")
@Slf4j
@Api(value = "SysNoticeController",tags = "发送信息布告表接口")
public class SysNoticeController {

    @DubboReference
    private ISysNoticeService sysNoticeService;


    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量逻辑删除",notes = "批量逻辑删除")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        List<SysNoticeDTO> sysNoticeDTOArrayList = new ArrayList<>();
        String[] arr = ids.split(",");
        for (String strId : arr) {
            SysNoticeDTO dto = new SysNoticeDTO();
            dto.setId (Long.parseLong (strId));
            dto.setDelFlag (1);
            sysNoticeDTOArrayList.add(dto);
        }
        if(sysNoticeDTOArrayList != null && sysNoticeDTOArrayList.size ()>0){
            boolean flag = sysNoticeService.updateBatchById (sysNoticeDTOArrayList);
            if(flag){
                return new Result<>(CodeEnum.SUCCESS);
            } else {
                return new Result<>(CodeEnum.FAIL_100009);
            }
        }
        return new Result<>();
    }


    @ApiOperation(value = "单独逻辑删除",notes = "单独逻辑删除")
    @ApiImplicitParam(name = "id",value = "发送信息布告表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        SysNoticeDTO dto = new SysNoticeDTO();
        dto.setId(id);
        dto.setDelFlag (1);
        boolean flag = sysNoticeService.updateById(dto);
        if(flag){
            return new Result<>(CodeEnum.SUCCESS);
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }


    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysNoticeDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        SysNoticeDTO dto = sysNoticeService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "发送信息布告表列表分页查询",notes = "发送信息布告表列表分页查询")
    @PostMapping("/queryPageList")
    public Result<Page<SysNoticeVO>> queryPageList(SysNoticeBO bo){
        return new Result<>(sysNoticeService.queryPageList(bo));
    }

}
